package cn.edu.csust.coursecard.controller;

import cn.edu.csust.coursecard.common.ReturnData;
import cn.edu.csust.coursecard.dao.entity.StuInfo;
import cn.edu.csust.coursecard.service.StuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zsw
 * @author 许大人
 * @version 2.0
 * @date 2019/11/18 21:40
 * @date 2022/08/11
 */
@RestController
public class StuInfoController extends BaseController {

    @Autowired
    StuInfoService stuInfoService;

    @PostMapping("/login")
    public ReturnData login(String stuNum, String password, String version) {
        return stuInfoService.loginThroughJW(stuNum, password, version);
    }

    @PostMapping("/getBasicData")
    public ReturnData getBasicData(String cookie) {
        return stuInfoService.getBasicData(cookie);
    }

    @PostMapping("/getWeekDate")
    public ReturnData getWeekDate(String cookie) {
        return stuInfoService.getWeekDate(cookie);
    }

    @PostMapping("/getWeekCourse")
    public ReturnData getWeekCourse(String cookie, @RequestParam("time") @DateTimeFormat(pattern = "yyyy-MM-dd") String weekTime) {
        return stuInfoService.getWeekCourse(cookie, weekTime);
    }

    @PostMapping("/getCourse")
    public ReturnData getCourse(String cookie, String xueqi, int zc) {
        return stuInfoService.getCourse(cookie, xueqi, zc);
    }

    @PostMapping("/getAllSemester")
    public ReturnData getAllSemester(HttpServletRequest request, String cookie) {
        return stuInfoService.getAllTermList(request, cookie);
    }

    //考试安排
    @PostMapping("/getKsap")
    public ReturnData getExamArrangement(String cookie, String xueqi) {
        return stuInfoService.getExamArrangement(cookie, xueqi);
    }

    @PostMapping("/queryScore")
    public ReturnData queryScore(String cookie, String xueqi) {
        return stuInfoService.getScoreList(cookie, xueqi);
    }

    //平时成绩
    @PostMapping("/queryPscj")
    public ReturnData queryUsualGrades(String cookie, String pscjUrl) {
        return stuInfoService.getScoreDetail(cookie, pscjUrl);
    }


    @PostMapping("/setHeadImg")
    public ReturnData setHeadImg(HttpServletRequest request, @RequestParam("img") MultipartFile file) {
        return stuInfoService.setStudentAvatar(request, file);
    }

    @PostMapping("/restoreHeadImg")
    public ReturnData restoreHeadImg(HttpServletRequest request, String cookie) {
        String imgUrl = stuInfoService.restoreJwAvatar((Integer) request.getAttribute("userId"), cookie, 1);
        return ReturnData.success(imgUrl);
    }

    @PostMapping("/getHeadImg")
    public ReturnData getHeadImg(HttpServletRequest request, String cookie) {
        String imgUrl = stuInfoService.getStudentAvatar((Integer) request.getAttribute("userId"), cookie);
        return ReturnData.success(imgUrl);
    }

    @PostMapping("/setStuInfo")
    public ReturnData setStuInfo(HttpServletRequest request, StuInfo stuInfo) {
        return stuInfoService.setStuInfo(request, stuInfo);
    }

    @PostMapping("/refreshStuInfo")
    public ReturnData getStuInfoFromJW(HttpServletRequest request, String cookie) {
        StuInfo stuInfo = stuInfoService.refreshStuInfoFromJW((Integer) request.getAttribute("userId"), cookie, null, null);
        return ReturnData.success(stuInfo);
    }

    @PostMapping("/getStuInfo")
    public ReturnData getStuInfoFromDB(HttpServletRequest request, Integer userId, String cookie) {
        Integer selfUserId = getUserIdFromRequest(request);
        StuInfo stuInfo = stuInfoService.getStuInfoFromDB(selfUserId, userId, cookie);
        return ReturnData.success(stuInfo);
    }

    @PostMapping("/getNoticeList")
    public ReturnData getNoticeList(String cookie) {
        return stuInfoService.getNoticeListFromJW(cookie);
    }

    @PostMapping("/getNoticeDetail")
    public ReturnData getNoticeDetail(String cookie, String ggid) {
        return ReturnData.success(stuInfoService.getNoticeDetail(cookie, ggid));
    }

}
